import json
import os
import time
import re
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def save_cookies(driver, path):
    dict_cookies = driver.get_cookies()
    json_cookies = json.dumps(dict_cookies)
    with open(path, 'w') as f:
        f.write(json_cookies)
    print('Cookies have been saved.')

def load_cookies(driver, path):
    with open(path, 'r') as file:
        cookies = json.load(file)
        for cookie in cookies:
            driver.add_cookie(cookie)
    print('Cookies loaded from file.')

def login_and_save_cookies(driver, url, cookie_file):
    driver.get(url)
    if os.path.exists(cookie_file):
        load_cookies(driver, cookie_file)
        driver.get("https://www.taobao.com/")
        time.sleep(2)
    else:
        time.sleep(20)  # Wait for manual login and save the cookies
        save_cookies(driver, cookie_file)

def setup_driver():
    chrome_options = Options()
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")
    driver = webdriver.Chrome(options=chrome_options)
    return driver

def search_and_collect_data(driver):
    driver.get("https://s.taobao.com/search?q=iphone")
    time.sleep(5)  # Wait for page to load
    data_array = []

    for p in range(1, 100):  # Example: iterate over 100 pages
        html = driver.page_source
        # Here you can process the HTML with BeautifulSoup or regex
        # For example, collecting data from product listings:
        # data_array.append(extract_data(html))
        # Move to the next page
        next_button = driver.find_element_by_css_selector('.next-icon.next-icon-arrow-right.next-xs.next-btn-icon.next-icon-last.next-pagination-icon-next')
        next_button.click()
        time.sleep(1)

    # Save data
    with open('data.csv', 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerows(data_array)

def main():
    driver = setup_driver()
    login_and_save_cookies(driver, "https://login.taobao.com/member/login.jhtml", 'cookie.txt')
    search_and_collect_data(driver)
    driver.quit()

if __name__ == "__main__":
    main()
